package com.smarthouse.controller;

import com.smarthouse.model.ApiResponse;
import com.smarthouse.model.FaceCompareRequest;
import com.smarthouse.model.FaceCompareResponse;
import com.smarthouse.service.FaceRecognitionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


//http://192.168.43.50/api/face/compare

@Tag(name = "人脸识别接口")
@RestController
@RequestMapping("/face")
@CrossOrigin
public class FaceRecognitionController {
    private static final Logger logger = LoggerFactory.getLogger(FaceRecognitionController.class);

    @Autowired
    private FaceRecognitionService faceRecognitionService;

    @Operation(summary = "人脸比对")
    @PostMapping("/compare")
    public ApiResponse<FaceCompareResponse> compareFaces(@RequestBody FaceCompareRequest request) {
        logger.info("收到人脸比对请求 - 请求路径: /api/face/compare");
        try {
            // 检查请求参数
            if (request == null || request.getImage1Base64() == null) {
                logger.error("请求参数为空");
                return ApiResponse.error("请求参数不能为空");
            }
            
            // 打印部分图片数据用于调试
            String image1Preview = request.getImage1Base64().substring(0, Math.min(100, request.getImage1Base64().length()));
            logger.info("收到图片数据(前100字符): {}", image1Preview);
            
            logger.info("开始调用百度人脸比对服务");
            FaceCompareResponse response = faceRecognitionService.compareFaces(
                request.getImage1Base64()
            );
            logger.info("人脸比对完成，相似度：{}", response.getSimilarity());
            
            return ApiResponse.success(response);
        } catch (Exception e) {
            logger.error("人脸比对失败", e);
            return ApiResponse.error(e.getMessage());
        }
    }
} 